mui.init();
mui.plusReady(function() {
	initRecode();
});

$(function() {
	//打开新增页面
	document.getElementById("open").addEventListener('tap', function() {
		$("#name").val("");
		$("#type").val("");
		$("#content").val("");
		$("#title").html("新增备忘");
		$("#add_div").removeClass("mui-hidden");
		$("#edit_div").addClass("mui-hidden");
		mui("#popover").popover("toggle");
	});
	
	//新增操作
	document.getElementById("add").addEventListener('tap', function() {
		var check = true;
		mui(".mui-input-group input").each(function() {
			if(!this.value || trim(this.value) == "") {
				var label = this.previousElementSibling;
				mui.alert(label.innerText + "不允许为空");
				check = false;
				return false;
			}
		});
		if(check) {
			var ref = sun.createLink("/recode");
			var newRef = ref.push({
				"name": $("#name").val(),
				"content": $("#content").val(),
				"dateval": new Date().getTime(),
				"userId": app.getUser().account
			});
			$("#add_div").addClass("mui-hidden");
			//关闭modal
			mui("#popover").popover("toggle");
			mui.toast("添加成功~");
		}
	});
	
	//更新
	document.getElementById("update").addEventListener("tap", function() {
		var check = true;
		mui(".mui-input-group input").each(function() {
			if(!this.value || trim(this.value) == "") {
				var label = this.previousElementSibling;
				mui.alert(label.innerText + "不允许为空");
				check = false;
				return false;
			}
		});
		if(check) {
			initParams()
			var ref = sun.createLink("/recode");
			var newRef = ref.child($("#name").attr("data-id"));
			newRef.update({
				"name": $("#name").val(),
				"content": $("#content").val(),
				"type": $("#type").val()
			});
			//关闭modal
			mui("#popover").popover("toggle");
			$("#edit_div").addClass("mui-hidden")
			mui.toast("修改成功~");
			//初试化缓存
			//加载未完成列表
			//初始化已完成列表
		}
	});
	//删除操作
	document.getElementById("del").addEventListener("tap", function() {
		var ref = sun.createLink("/recode");
		var newRef = ref.child($("#name").attr("data-id"));
		newRef.remove();
		//关闭modal
		mui("#popover").popover("toggle");
		$("#edit_div").addClass("mui-hidden");
		mui.toast("删除成功~");
		//刷新界面
		//初试化缓存
		//加载未完成列表
		//初始化已完成列表
	});
});

function trim(str) { //删除左右两端的空格
	return str.replace(/(^\s*)|(\s*$)/g, "");　　
}

function initRecodes() {
	var ref = sun.createLink("/recode");
	var recodes = [];
	ref.on("value", function(snapshot) {
		var result = snapshot.val();
		mui.each(result, function(key, item) {
			if(item.userId == app.getUser().account) {
				var recode = {};
				recode = sun.cloneObj(item);
				recode.id = key;
				recodes.push(recode);
			}
		});
		recodes.sort(function(oa, ob) {
			var oDate1 = new Date(oa.dateval);
			var oDate2 = new Date(ob.dateval);
			if(oDate1.getTime() > oDate2.getTime()) {
				return -1;
			} else if(oDate1.getTime() < oDate2.getTime()) {
				return 1
			}
		});
		localStorage.setItem("$recodes", JSON.stringify(recodes));
	});
}

function initList() {
	var $ul = $('#recodeList').empty();
	if (localStorage.getItem("$recodes") == null) {
		initRecodes();
	}
	var array = JSON.parse(localStorage.getItem("$recodes"));
	for(var o in array) {
		var li = "";
		var data = array[o];
		li += '<li class="mui-table-view-cell mui-collapse" data-id="' + data.id + '">';
		li += '<div class="mui-navigate-right">';
		li += '<span>' + data.name + '</span>';
		li += '<p>' + data.type + ':' + data.money + '元</p>';
		li += '<input type=hidden id="name_" value="' + data.name + '"/>';
		li += '<input type=hidden id="content_" value="' + data.content + '"/>';
		li += '<input type=hidden id="dateval_" value="' + data.dateval + '"/>';
		li += '</div>';
		li += '</li>';
		$ul.append(li);
	}
	if($ul.find('li').size() > 0 && $ul.is(':hidden')) $ul.show();
}
